<?php

Zend_Loader::loadClass("Application_Plotter_Base");
Zend_Loader::loadClass('Output_ReportVariableData');
Zend_Loader::loadClass('Output_ReportVariableDataDictionary');
Zend_Loader::loadClass('Application_IndiceEnergetico');

class Application_Plotter_IndiciPrestazioneEnergetica extends Application_Plotter_Base
{
	public function call() {
		
		$stmt = $this->_db->query('
			SELECT SUM(VariableValue) as sommaRaffrescamento
			FROM ReportVariableData
			INNER JOIN ReportVariableDataDictionary ON ReportVariableData.ReportVariableDataDictionaryIndex=ReportVariableDataDictionary.ReportVariableDataDictionaryIndex
			WHERE ReportVariableDataDictionary.VariableName = ?
		', array ("Zone/Sys Sensible Cooling Energy"));
		$resultSommaRaffrescamento = $stmt->fetchAll($query);
		$indiciPrestazioniEnergetiche = new Application_IndiceEnergetico();
		$indiciPrestazioniEnergetiche->setEnergiaRaffrescamento($resultSommaRaffrescamento[0]['sommaRaffrescamento']);
		
		
		$stmt = $this->_db->query('
					SELECT SUM(VariableValue) as sommaRiscaldamento
					FROM ReportVariableData
					INNER JOIN ReportVariableDataDictionary ON ReportVariableData.ReportVariableDataDictionaryIndex=ReportVariableDataDictionary.ReportVariableDataDictionaryIndex
					WHERE ReportVariableDataDictionary.VariableName = ?
				', array ("Zone/Sys Sensible Heating Energy"));
		$resultSommaRiscaldamento = $stmt->fetchAll($query);
		$indiciPrestazioniEnergetiche->setEnergiaRiscaldamento($resultSommaRiscaldamento[0]['sommaRiscaldamento']);
		
		/*
		$stmt = $this->_db->query('
							SELECT Area
							FROM Surfaces
							WHERE ClassName = ?
						', array ("Floor"));
		$resultsSuperficie = $stmt->fetchAll($query);
		$indiciPrestazioniEnergetiche->setSuperficie($resultsSuperficie[0]['Area']);
		*/
	   
		
		//inizializzo un array che mi rappresenta le mura della stanza
		$wall=array();
		//seleziono tutte le mura della stanza, restituendo il relativo Construction Index e larghezza)
		
		$stmt = $this->_db->query(' SELECT ConstructionIndex , Width
									FROM Surfaces
									WHERE ClassName = ?
								', array ("Wall"));
		$resultsWall = $stmt->fetchAll($query);
	    if (!empty($resultsWall)) {
	    	$i=0;
			foreach ($resultsWall as &$result) {
				$wall[$i][0]= $result['ConstructionIndex'];
				$wall[$i][1] = $result['Width'];
				$i++;
			}
		}
		//Zend_Debug::dump($wall); exit;
		
	    //scorro l'array delle mura della stanza (constructionIndex) ed eseguo la query per conoscere
	    //lo spessore di ogni muro

		for($i=0; $i<count($resultsWall); $i++){
			$stmt = $this->_db->query(' SELECT Sum(Thickness) as Spessore
										FROM Materials, ConstructionLayers
										WHERE ConstructionLayers.MaterialIndex=Materials.MaterialIndex
										AND ConstructionIndex = ?
									', array ($wall[$i][0]));
			$resultSpessore = $stmt->fetchAll($query);
			$wall[$i][2]=$resultSpessore[0]['Spessore'];
		}
			
		//ora ho tutti i dati per poter calcolare la superficie netta 
		$lWallUno=$wall[0][1]-$wall[1][2]-$wall[3][2];
		$lWallDue=$wall[3][1]-$wall[0][3]-$wall[2][2];
		
		$superficieNetta=$lWallUno*$lWallDue;
		$indiciPrestazioniEnergetiche->setSuperficie($superficieNetta);
		
		//Zend_Debug::dump($wall); exit;
		
		return $indiciPrestazioniEnergetiche;
	}	
	
}
